package Z.dailyExercise.March;

public class _303区域和检索数组不可变 {

    /**
     * 遍历
     */
    int[] nums;
//    public _303区域和检索数组不可变(int[] nums) {
//        this.nums = nums;
//    }

    public int sumRange(int i, int j) {
        int res =0;
        for (int k = i; k <= j ; k++) {

            res += nums[k];


        }

        return res;

    }

    /**
     * 前缀和,sum从
     */

    int sum[];
    public _303区域和检索数组不可变(int[] nums) {
        sum = new int[nums.length+1];
        for (int i = 0; i < nums.length; i++) {
            sum[i+1] = sum[i]+nums[i];

        }
    }

    public int sumRange1(int i, int j) {

        return sum[j+1]- sum[i];

    }

}
